package com.daykw.xiaohongshu.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.nutz.dao.impl.NutDao;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.stereotype.Service;

import com.daykw.xiaohongshu.http.HttpBase;
import com.daykw.xiaohongshu.model.TSessionId;
import com.daykw.xiaohongshu.util.SpringContextHolder;

import test.XiaoHongShuSign;

@DisallowConcurrentExecution
@Service
public class SessionJob implements Runnable {

	private static Logger logger = Logger.getLogger(SessionJob.class);

	@Override
	public void run() {
		int COUNT=500;
		NutDao dao = SpringContextHolder.getBean("dao");
		HttpBase http = new HttpBase();
		String url = "https://www.xiaohongshu.com/api/sns/v1/user/activate";
		Map<String,String> header = null;
		Map<String,String> params = null;
		XiaoHongShuSign xhss = new XiaoHongShuSign();
		DateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		java.util.Random r = new Random();
		for(int i=0;i<COUNT;i++) {
			String device_id = UUID.randomUUID().toString();
			String android_id = "507b9d7c370c5488";
			String imei = ""+(Math.abs(r.nextLong())*866174010912984L+100000000000000L);
			String platform = "Android";
			String device_fingerprint = f.format(new Date())+"b2d01289b09f5de7"+UUID.randomUUID().toString().replaceAll("-", "");
			String versionName="5.23.0";
			String channel="Store360";
			String lang="zh-Hans";
			String t=""+(System.currentTimeMillis()/1000);
			String sign="";
			try {
				header = new HashMap<String,String>();
				header.put("Authorization","");
				header.put("device_id",device_id);
				header.put("User-Agent","Dalvik/1.6.0 (Linux; U; Android 4.4.2; MI 6  Build/NMF26X) Resolution/720*1280 Version/5.23.0 Build/5230179 Device/(Xiaomi ;MI 6 )");
				header.put("shield","");
				header.put("Content-Type","application/x-www-form-urlencoded");
				header.put("Host","www.xiaohongshu.com");
				header.put("Accept-Encoding","gzip");
				//android_id=507b9d7c370c5488&imei=866174010912984&platform=Android&deviceId=fa8265c4-f929-3900-90bf-ac6d321cc6ab&device_fingerprint=20180705144829b2d01289b09f5de74eb538aa504475320139e69c689426dc&device_fingerprint1=20180705144829b2d01289b09f5de74eb538aa504475320139e69c689426dc&versionName=5.23.0&channel=Store360&lang=zh-Hans&t=1536425134&sign=68adc2100f380d4ec13413ff680bcc26
				String sb = "android_id="+android_id+"&imei="+imei+"&platform="+platform+"&deviceId="+device_id+"&device_fingerprint="+device_fingerprint+"&device_fingerprint1="+device_fingerprint+"&versionName="+versionName+"&channel="+channel+"&lang="+lang+"&t="+t;
				sign=xhss.sign(sb);
				params = new HashMap<String,String>();
				params.put("android_id", android_id);
				params.put("imei", imei);
				params.put("platform", platform);
				params.put("deviceId", device_id);
				params.put("device_fingerprint", device_fingerprint);
				params.put("device_fingerprint1", device_fingerprint);
				params.put("versionName", versionName);
				params.put("channel", channel);
				params.put("lang", lang);
				params.put("t", t);
				params.put("sign", sign);
				String result = http.post(url, header, params,true);
				JSONObject json = new JSONObject(result);
				JSONObject data = json.getJSONObject("data");
				String sid = "session."+data.getString("session");
				String user_token = data.getString("user_token");
				String userid = data.getString("userid");
				TSessionId ts = new TSessionId();
				ts.setAndroid_id(android_id);
				ts.setImei(imei);
				ts.setPlatform(platform);
				ts.setVersionName(versionName);
				ts.setChannel(channel);
				ts.setLang(lang);
				ts.setT(t);
				ts.setSign(sign);
				ts.setPs(sb);
				
				ts.setCreate_time(new Date());
				ts.setDevice_fingerprint(device_fingerprint);
				ts.setDeviceId(device_id);
				ts.setSid(sid);
				ts.setStatus(0);
				ts.setUser_token(user_token);
				ts.setUserid(userid);
				dao.fastInsert(ts);
				logger.info("生产sessionid成功:"+sid);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				logger.error("生产sessionid报错:",e1);
			}
		}

	}

}
